using System.Collections;
using System.Collections.Generic;
using Main.Event;
using UnityEngine;
using ZSW.Framework;

namespace Main.CropPlant
{
    /// <summary>
    /// 为Item提供收获植物和掉落物品的职能,类收割果实
    /// </summary>
    public class ReapItem : MonoBehaviour
    {
        private CropDetails cropDetails;

        public void SetData(int id)
        {
            ZSWF_IOCContainer.Get<CropManager>().TryGetCropDetails(id, out cropDetails);
        }

        /// <summary>
        /// 生成农作物
        /// </summary>
        public void SpawnHarvestItems()
        {
            for (int i = 0; i < cropDetails.productItemIDs.Length; i++)
            {
                int itemID = cropDetails.productItemIDs[i];
                int minAmount = cropDetails.productItemMinAmounts[i];
                int maxAmount = cropDetails.productItemMaxAmounts[i];

                int amount = UnityEngine.Random.Range(minAmount, maxAmount + 1);

                // 循环生成指定数量的物品
                for (int j = 0; j < amount; j++)
                {
                    //
                    if (cropDetails.generateAtPlayPosition)
                    {
                        MainEventSystem.HarvestAtPlayerPositionEvent(itemID);
                    }
                    // 世界中生成物品
                    else
                    {
                        var dirX = transform.position.x - Player.Transform.position.x > 0 ? 1 : -1;

                        var spawnPos = new Vector3(transform.position.x + UnityEngine.Random.Range(0f, cropDetails.spawnRadius.x * dirX),
                        transform.position.y + UnityEngine.Random.Range(-cropDetails.spawnRadius.y, cropDetails.spawnRadius.y), 0);

                        MainEventSystem.CallInstantiateItemEvent(cropDetails.productItemIDs[i], spawnPos);
                    }
                }
            }

            Destroy(this.gameObject);
        }
    }
}
